package coint.btc.controller.auth;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import coint.btc.dto.PermissionDTO;
import coint.btc.service.auth.PermissionService;
import coint.btc.util.HttpStatus;
import coint.btc.util.WriteJson;
import coint.btc.validator.MergeGroups;
import coint.btc.validator.SaveGroups;
import coint.btc.vo.Menu;
import coint.btc.vo.Page;
import coint.btc.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



/***
 * 
 * @Title: DaozhaJueseQuanxianController
 * 
 * @Description:
 * 
 * @author wyx
 * 
 * @date 2018年7月18日
 */

@Api(value = "角色权限设置controller", tags = { "角色权限设置操作接口" })
@RestController
@RequestMapping("/console/quanxian")
public class JuesePermissionController {

	@Autowired
	private PermissionService quanxservice;

	/***
	 * 给角色分配权限
	 * 
	 * @Title: addJueseQuanxian
	 * 
	 * @Description:
	 * 
	 * @author wyx
	 * 
	 * @date 2018年7月18日
	 * 
	 * @param id
	 * @param ids
	 * @return
	 */
	@PostMapping("/distribution/add")
	@ApiOperation(value = "给角色分配权限")
	public ModelMap addJueseQuanxian(@RequestParam(value = "id") Integer id,
			@RequestParam(value = "ids[]") Integer[] ids) {
		
		
		if(id==null) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "请选择角色id", "");
		}
		
		if(ids.length<=0) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "请选择页面权限id", "");
		}
	
		boolean flag = quanxservice.addJueseQuanxian(id, ids);

		if (!flag) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "分配权限失败", "");
		}

		return WriteJson.successMsg("分配权限成功");

	}
	
	/***
	 * 
	
	 * @Title: addJueseQuan 
	
	 * @Description:
	 
	 * @author wyx  
	
	 * @date 2018年7月18日  
	
	 * @param quanx
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加角色权限")
	public ModelMap addJueseQuan(@Validated(value = { SaveGroups.class }) PermissionDTO  quanx , BindingResult result) {
		
		if(result.hasErrors()) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "添加失败", result.getFieldError().getDefaultMessage());
		}

		int flag = quanxservice.addQuanXian(quanx);
		
		if (flag<=0) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "添加页面菜单失败", "");
		}

		return WriteJson.successMsg("添加页面菜单成功");

	}
	
	
	/***
	 * 
	
	 * @Title: checkJueseQuan 
	
	 * @Description:检测是否有同名称菜单
	 
	 * @author wyx  
	
	 * @date 2018年7月18日  
	
	 * @param quanx
	 * @return
	 */
	@PostMapping("/check")
	@ApiOperation(value = "检测是否有同名称菜单")
	public ModelMap checkJueseQuan(PermissionDTO  quanx) {

		boolean  flag = quanxservice.check(quanx);
		
		if (!flag) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "添加页面菜单或目录失败 ,菜单名不能重复", "");
		}

		return WriteJson.successMsg("添加页面菜单成功");

	}
	
	
	/**
	 * 
	 * @Description:  获取用户操作菜单
	 * @date:2018年7月18日   
	 * @author: lt
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/menu")
	@ApiOperation(value = "用户操作权限菜单")
	public ModelMap getMenu()  {
		List<Menu> menus=quanxservice.menu();
		return WriteJson.successData(menus);
	}
	
	
	
	//新增权限
	@PostMapping("/console/quanxian/auth/save")
	@ApiOperation(value = "新增权限菜单")
	public ModelMap saveAuth(@Validated(value = {SaveGroups.class}) PermissionDTO  auth) {
		int flag = quanxservice.addQuanXian(auth);
		if(flag>0) {
			return WriteJson.successData("添加成功");
		}
		return WriteJson.successData(" 添加成功");
	}
	//修改权限
	@PostMapping("/console/quanxian/auth/upd")
	@ApiOperation(value = "修改权限菜单")
	public ModelMap updateAuth(@Validated(value = {MergeGroups.class}) PermissionDTO auth) {
		int flag = quanxservice.updatePermission(auth);
		if(flag>0) {
			return WriteJson.successData("修改成功");
		}
		 return WriteJson.successData("修改失败");
	}
	//删除权限
	@ApiOperation(value = "删除权限菜单")
	@GetMapping("/console/quanxian/auth/del/{authId}")
	public ModelMap deleteAuth(@PathVariable("authId") Integer authId) {
		int flag =quanxservice.deletePerMission(authId);
		if(flag>0) {
			return WriteJson.successData("删除成功");
		}
		
		return WriteJson.successData("删除失败");
	}
	//权限列表
	@PostMapping("/console/quanxian/auth/page")
	@ApiOperation(value = "权限列表")
	public ModelMap page(PermissionDTO auth) {
		 Page<PermissionVo> page = new Page<PermissionVo>();
		page.setPage(auth.getPage());
		page.setRows(auth.getRows());
		
		
		return WriteJson.successDataMsg("查询成功", quanxservice.page(auth, page));
	}
	
	
	
	
}
